# 随机生成了资源不同模式cost
import sys
from collections import defaultdict

import Activity as A
import numpy as np

actNum = 0  # 声明变量
modeNum = 0  # 声明变量


def read(fileName):
    """
    每种资源不同模式下的花费在此随机
    读取算例
    :return:
    actNum:活动总数
    actList:活动列表
    resourceNum:资源种数
    resources:资源量
    """
    # --------从文件读取数据--------
    # fileName = input("输入算例文件名****.RCP")
    # fileName = f"data/n01_2.mm"
    f = open(fileName)
    global modeNum
    global actNum
    lineNum = 0
    while True:
        str = f.readline()
        lineNum += 1
        if str.startswith("jobs (incl. supersource/sink )"):
            # print(str)
            actNumLine = str.split()
            actNum = int(actNumLine[-1])
            break
    # print(actNum)
    # sys.exit()
    for i in range(6, 8):
        f.readline()
        lineNum += 1
    resourceNumLine = f.readline().split()
    lineNum += 1
    resourceNum = int(resourceNumLine[-2])
    # print("resourcesNum :", resourceNum)
    actList = {}
    preList = defaultdict(lambda: [])  # 紧前活动
    while True:
        str = f.readline()
        lineNum += 1
        if str.startswith("PRECEDENCE RELATIONS:"):
            break
    f.readline()
    lineNum += 1
    for i in range(0, actNum):
        line = f.readline().split()  # 文件中一行
        lineNum += 1
        format_line = list(map(int, line))
        # print(format_line)
        # sys.exit()
        act = A.Activity(format_line[0], format_line[1], format_line[2], format_line[3:])
        # print(act.id)
        actList[format_line[0]] = act
        for j in format_line[3:]:
            preList[j].append(format_line[0])
    # print(lineNum)
    for k in actList.keys():
        # print(preList[actList[k].id])
        actList[k].preList = preList[actList[k].id].copy()
    # print(actList.keys())
    start = lineNum
    while True:
        str = f.readline()
        lineNum += 1
        if str.startswith("REQUESTS/DURATIONS:"):
            f.readline()
            f.readline()
            break
    modes = {}
    modeNum = actList[actNum - 1].modeNum
    line = f.readline().split()
    lineNum += 1
    firstline = list(map(int, line))
    # print(line[1:])
    # sys.exit()
    actList[1].modes = firstline[1:]
    # print(modeNum)
    for i in range(0, (actNum - 2) * modeNum + 1):
        line = f.readline().split()
        lineNum += 1
        # print(line)
        line = list(map(int, line))
        if len(line) == resourceNum + 3:
            # print(actList[line[0]].id)
            actList[line[0] - 1].modes = modes
            # print(line[0]-1)
            # print(modes)
            modes = {}
            modes[line[1]] = line[2:]
        else:
            modes[line[0]] = line[1:]
        modeLine = line[-3:-1]
        # print(modeLine)
    actList[1].modes[1] = firstline[2:]
    actList[actNum].modes = modes
    # """actList modes验证"""
    # print("actMode reading result")
    # for i in range(1, actNum+1):
    #     print(actList[i].id)
    #     print(actList[i].modes)
    # sys.exit(99999)
    # print(lineNum)
    # print(actList[12].modes.values())
    start = lineNum
    while True:
        str = f.readline()
        lineNum += 1
        if str.startswith("RESOURCEAVAILABILITIES:"):
            f.readline()
            lineNum += 1
            break
    resources = f.readline().split()
    resources = list(map(int, resources))
    # print(resources)
    f.readline()
    if f.readline().startswith("cost"):
        cost = {}
        line = f.readline().split()
        lineNum += 1
        firstline = list(map(int, line))
        # print(line[1:])
        # sys.exit()
        actList[1].cost = firstline[1]
        for i in range(0, (actNum - 2) * modeNum + 1):
            line = f.readline().split()
            lineNum += 1
            # print(line)
            line = list(map(int, line))
            if len(line) == resourceNum + 2:
                # print(actList[line[0]].id)
                actList[line[0] - 1].cost = cost
                # print(line[0]-1)
                # print(modes)
                cost = {}
                cost[line[1]] = line[2]
            else:
                cost[line[0]] = line[1]
            # modeLine = line[-3:-1]
            # print(modeLine)
        actList[1].cost[1] = firstline[2]
        actList[actNum].cost = cost
    else:
        f.close()
        """
        算例中未包含资源成本
        随机生成资源成本
        """
        actList[1].cost = {1: 0}
        np.random.seed(12)
        for i in range(2, actNum):
            # print(i)
            cost = {}
            for j in range(1, 4):
                cost[j] = np.random.randint(1, 50)
                # print(cost[j])
            actList[i].cost = cost
            # print(actList[i].cost)
        actList[actNum].cost = {1: 0}
    """打印读入结果"""
    # for act in actList.values():
    #     print(act)
    return actNum, actList, resourceNum, resources, modeNum


if __name__ == '__main__':
    read()
